package com.frinder.astar;

import java.util.List;

/**
 * Created by kiktech on 2018/2/10.
 */
public class AStar {

    private int[][] map;

    private int width;

    private int height;

    private Node sourceNode;

    private Node destNode;

    private AStar(Builder builder) {
        width = builder.width;
        height = builder.height;
        sourceNode = builder.sourceNode;
        destNode = builder.destNode;
        initMap();
    }

    /**
     * 设置路径
     *
     * @param points
     */
    public void setPath(List<Point> points) {
        points.stream().forEach(point -> map[point.getY()][point.getX()] = Constants.PATH);
    }

    /**
     * 设置 block
     *
     * @param blockList
     */
    public void setBlocks(List<Point> blockList) {
        blockList.stream().forEach(o -> map[o.getY()][o.getX()] = Constants.BAR);
    }

    /**
     * 初始化 map
     */
    private void initMap() {
        map = new int[height][width];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                map[y][x] = 0;
            }
        }
    }

    public int[][] getMap() {
        return map;
    }

    public static final class Builder {
        private int width;
        private int height;
        private Node sourceNode;
        private Node destNode;

        public Builder() {
        }

        public Builder width(int val) {
            width = val;
            return this;
        }

        public Builder height(int val) {
            height = val;
            return this;
        }

        public Builder sourceNode(Node val) {
            sourceNode = val;
            return this;
        }

        public Builder destNode(Node val) {
            destNode = val;
            return this;
        }

        public AStar build() {
            return new AStar(this);
        }
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public Node getSourceNode() {
        return sourceNode;
    }

    public Node getDestNode() {
        return destNode;
    }
}
